clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use if qruf1<. using $Work/EBR_new_monthly, clear

tsset permno datem

* compute actual returns
generate   retQr1=1
forvalues x=1(1)5 {
generate   retYr`x'=1
}

generate  retMo1=ret_mo1
forvalues x=1(1)3 {
replace   retQr1=retQr1*(ret_mo`x')
}
forvalues x=1(1)12 {
replace   retYr1=retYr1*(ret_mo`x')
}
forvalues x=13(1)24 {
replace   retYr2=retYr2*(ret_mo`x')
}
forvalues x=25(1)36 {
replace   retYr3=retYr3*(ret_mo`x')
}
forvalues x=37(1)48 {
replace   retYr4=retYr4*(ret_mo`x')
}
forvalues x=49(1)60 {
replace   retYr5=retYr5*(ret_mo`x')
}
* *************************
scalar alfa= 0.97749304
gen cumRetNewMo1=ln(retMo1) 
gen cumRetNewQr1=ln(retQr1) 
gen cumRetNewYr1=ln(retYr1) 
gen cumRetNewYr3=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3) 
gen cumRetNewYr5=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3)+alfa^3*ln(retYr4)+alfa^4*ln(retYr5)
* **************************
gen cumQNRetMo1=ln(1+qnic1 ) 
gen cumQNRetQr1=ln(qnic_qr1) 
gen cumQNRetYr1=ln(qnic_yr1) 
gen cumQNRetYr3=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3) 
gen cumQNRetYr5=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3)+alfa^3*ln(qnic_yr4)+alfa^4*ln(qnic_yr5)
gstats winsor cumQN*, cuts(1 99) by(datem) replace 
* **************************
keep permco permno datem cum*
tempfile cumQ
save "`cumQ'", replace
* **************************
merge 1:1 permco permno datem using $Work/Firms01_June
keep if _merge==3
drop _merge
drop *LTG LTG
* from read_compustat_qtly.do
merge 1:1 permco permno datem using $Work/street_mthly, keepusing(permco permno datem streetLTM)

tsset permno datem
gen ge1Mo=ln(F01.streetLTM)-ln(streetLTM)
gen ge1Qr=ln(F03.streetLTM)-ln(streetLTM)
gen ge1Yr=ln(F12.streetLTM)-ln(streetLTM)
gen ge3Yr=ln(F36.streetLTM)-ln(streetLTM)
gen ge5Yr=ln(F60.streetLTM)-ln(streetLTM)
drop if _merge==2
drop _merge
* *****************************************************
merge 1:1 permco permno statpers using  $Work/XSibes.dta, keepusing(permco permno statpers LTG datem)

tsset permno datem

replace LTG=LTG/100
gen L1LTG=L12.LTG

gen dMFLTG=F1.LTG-LTG
gen dQFLTG=F3.LTG-LTG

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
}
drop _merge
save $Work/AppendixB10_June, replace
* *****************************************************
keep if BOOK+SIZE+LTG<.
* *****************************************************
foreach var of varlist FE1 FE3 FE5 cp* d*FLTG ge* {
	quietly: bysort datem: egen max=pctile(`var'), p(99)
	quietly: bysort datem: egen min=pctile(`var'), p(1)  
	quietly: replace `var'=max if `var'>max & `var'<.
	quietly: replace `var'=min if `var'<min	
	drop max min 
}
foreach var of varlist FE1 FE3 FE5 cp1 cp3 cp5 d1FLTG d3FLTG d5FLTG ge* {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
tsset permno datem
eststo clear
generate cumR=cumRetNewMo1
eststo:  quietly: ivreghdfe cumQNRetMo1 cumR, dkraay(1 ) a(permco)

replace  cumR=cumRetNewQr1
eststo:  quietly: ivreghdfe cumQNRetQr1 cumR, dkraay(4 ) a(permco)

replace  cumR=cumRetNewYr1
eststo:  quietly: ivreghdfe cumQNRetYr1 cumR, dkraay(13) a(permco)

replace  cumR=cumRetNewYr3
eststo:  quietly: ivreghdfe cumQNRetYr3 cumR, dkraay(37) a(permco)

replace  cumR=cumRetNewYr5
eststo:  quietly: ivreghdfe cumQNRetYr5 cumR, dkraay(61) a(permco)

local file "$Tables/AppendixB10_June.csv"
esttab using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) replace
* ******************************************************************************
eststo clear
generate FE=1
generate GE=ge1Mo
replace  cumR=cumRetNewMo1
eststo:  quietly: ivreghdfe cumQNRetMo1 cumR GE FE, dkraay(1 ) a(permco)

replace  GE=ge1Qr
replace  cumR=cumRetNewQr1
eststo:  quietly: ivreghdfe cumQNRetQr1 cumR GE FE, dkraay(4 ) a(permco)

replace  FE=FE1
replace  GE=ge1Yr
replace  cumR=cumRetNewYr1
eststo:  quietly: ivreghdfe cumQNRetYr1 cumR GE FE, dkraay(13) a(permco)

replace  FE=FE3
replace  GE=ge3Yr
replace  cumR=cumRetNewYr3
eststo:  quietly: ivreghdfe cumQNRetYr3 cumR GE FE, dkraay(37) a(permco)

replace  FE=FE5
replace  GE=ge5Yr
replace  cumR=cumRetNewYr5
eststo:  quietly: ivreghdfe cumQNRetYr5 cumR FE GE, dkraay(61) a(permco)
esttab using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a F) append
* ******************************************************************************
